//
//  HomeViewController.m
//  MyFishHelper
//
//  Created by EmberWei on 2018/6/15.
//  Copyright © 2018年 EmberWei. All rights reserved.
//

#import "HomeViewController.h"
#import <zhTableViewAnimations.h>

@interface HomeViewController ()

@property (nonatomic,strong) UserInfo *userInfo;
@property (nonatomic,strong) Fish *fish;
@property (nonatomic,strong) Adventure *adventure;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;

@property (weak, nonatomic) IBOutlet UILabel *powLabel;
@property (weak, nonatomic) IBOutlet UILabel *ethLabel;
@property (weak, nonatomic) IBOutlet UILabel *healthLabel;
@property (weak, nonatomic) IBOutlet UIImageView *countryIcon;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet UILabel *amountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *mainIcon;
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;

@property (weak, nonatomic) IBOutlet UILabel *health2Label;
@property (weak, nonatomic) IBOutlet UILabel *healthRemainLabel;
@property (weak, nonatomic) IBOutlet UIImageView *oceanIcon;
@property (weak, nonatomic) IBOutlet UILabel *fubenNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *stoneIcon;
@property (weak, nonatomic) IBOutlet UILabel *stoneDesc;
@property (weak, nonatomic) IBOutlet UILabel *ethDesc;

@property (weak, nonatomic) IBOutlet UILabel *mainFishPowerLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFishRankLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFishStoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFishLevelLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFishAreaLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFishNextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *mainFishAreaIcon;
@property (weak, nonatomic) IBOutlet UIImageView *mainFishNextIcon;
@property (weak, nonatomic) IBOutlet UIImageView *mainFishHeadIcon;
@property (weak, nonatomic) IBOutlet UILabel *mainFishNickNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *mainFishStoneIcon;
@property (weak, nonatomic) IBOutlet UILabel *mainFishMiniLabel;



@property (weak, nonatomic) IBOutlet UIButton *searchButtonOne;
@property (weak, nonatomic) IBOutlet UIButton *searchButtonAll;

@property (weak, nonatomic) IBOutlet UIButton *eatButtonOne;
@property (weak, nonatomic) IBOutlet UIButton *eatButtonAll;

@property (nonatomic,strong) NSNumber *leftEatFishCount;
@property (nonatomic,strong) NSNumber *leftSearchCount;
@property (weak, nonatomic) IBOutlet UIButton *jinghuaButton;


@property (nonatomic,assign) BOOL isPowerOK;
@property (nonatomic,assign) BOOL isStoneOK;
@property (nonatomic,assign) BOOL isEatOK;

@property (nonatomic,strong) FisheryIndex *fishIndex;

@property (nonatomic,strong) NSNumber *miniFishCount;
@property (nonatomic,strong) NSNumber *miniFishPrice;

@end

@implementation HomeViewController

- (IBAction)actionSearchOne:(id)sender {
    [self requestUserInfo];
    WEAK(self)
    [Api apiAdventure:self.adventure.oceand_id handler:^(ResponseGoAdventure *result, NSError *error) {
        STRONG(self)
        if (error) {
             [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            if ([result.code isEqualToString:@"1"]) {
                [self requestCheckETH];
            }
            return;
        }
        NSMutableString *string = [[NSMutableString alloc]initWithString:@"探索成功!获得:"];
        NSArray *array = result.data.item;
        for (Item *item in array) {
            [string appendString:@""];
            [string appendString:item.name];
            [string appendString:@":"];
            [string appendString:item.item_num];
        }
        [MFHUDManager showSuccess:string];
        [self requestAll];
    }];
}

- (IBAction)actionSearchAll:(id)sender {

    [self requestUserInfo];
    
    __block NSInteger cnt = [self.leftSearchCount integerValue];
    __block NSInteger item_num = 0;
    
    if (cnt ==0 ) {
        [MFHUDManager showError:@"体力不足"];
        [self requestCheckETH];
    }
    
    [MFHUDManager showLoading:@"探索中...."];
    
    for (int i = 0;  i < [self.leftSearchCount integerValue]; i++ ) {
        WEAK(self)
        [Api apiAdventure:self.adventure.oceand_id handler:^(ResponseGoAdventure *result, NSError *error) {
            STRONG(self)
            if (error) {
                [MFHUDManager showError:@"请检查网络,稍后再试"];
                return;
            }
            if (![result.code isEqualToString:@"0"]) {
                [MFHUDManager showError:result.msg];
                return;
            }
            cnt--;
            Item *item =  result.data.item.firstObject;
            item_num += [item.item_num integerValue];
            if (cnt == 0) {
                NSMutableString *string = [[NSMutableString alloc]initWithString:@"一键探索成功!获得:"];
                [string appendString:FORMAT(@"%@:%@",item.name,@(item_num))];
                [MFHUDManager showSuccess:string];
                [self requestAll];
            }
        }];
    }
}

- (void)requestAll {
    [self requestUserInfo];
    [self requestFishIndex];
    [self requestMyFishList];
    [self requestOcean];

}

-(void)reqeustMiniFishInfo
{
    WEAK(self)
    [Api apiMyMiniFishNum:self.fishIndex.fishery.fishery_id handler:^(ResponseMyFishList *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        self.miniFishCount = result.data.myfish_num;
        self.miniFishPrice = result.data.fish_price;
        self.amountLabel.text = FORMAT(@"鱼苗  %@ 条",self.miniFishCount);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"WechatIMG5.jpg"]];
    self.tableView.zh_reloadAnimationType = zhTableViewAnimationTypeSlideFromRight;
    WEAK(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        STRONG(self)
         [self requestAll];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(NSNumber*)getMaxOceanID:(NSNumber*)power
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"tansuo" ofType:@"json"];
    NSURL *url = [[NSURL alloc]initFileURLWithPath:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    AdventureList *adventureList = [JsonAdapter objectFromJsonData:data objectClass:[AdventureList class]];
    for (Adventure *adventure in adventureList.data) {
        NSNumber *oceanId = adventure.oceand_id;
        NSInteger fightPower = [adventure.fight_power integerValue];
        NSInteger nowPower = [power integerValue];
        if (nowPower < fightPower) {
            NSInteger last = [oceanId integerValue] -1;
            return @(last);
        }
    }
    return @(1);
}

-(Adventure*)fectchRecmondAdventure:(NSNumber*)power {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"tansuo" ofType:@"json"];
    NSURL *url = [[NSURL alloc]initFileURLWithPath:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    AdventureList *adventureList = [JsonAdapter objectFromJsonData:data objectClass:[AdventureList class]];
    NSInteger resultIndex = 0;
    for (int i = 0 ; i < adventureList.data.count ; i++) {
        Adventure *adventure = adventureList.data[i];
        NSInteger fightPower = [adventure.fight_power integerValue];
        NSInteger nowPower = [power integerValue];
        if (nowPower < fightPower) {
            resultIndex = i -1;
            if (resultIndex == -1) {
                resultIndex = 0;
            }
            break;
        }
    }
    return adventureList.data[resultIndex];
}

-(void)requestOcean {
    WEAK(self)
    [Api apiOceanHandler:^(ResponseOcean *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        self.healthLabel.text = FORMAT(@"%@",result.tili);
        self.health2Label.text = FORMAT(@"%@",result.tili);
        NSInteger remain = [result.tili integerValue] / 6;
        self.healthRemainLabel.text = FORMAT(@"%td 次",remain);
        [self.searchButtonAll setTitle:FORMAT(@"探索(%td)",remain) forState:UIControlStateNormal];
        self.leftSearchCount = @(remain);
    }];
}

-(void)requestMyFishList {
    WEAK(self)
    [Api apiMyFishHandler:^(ResponseMyFishList *result, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        if (result.data.list.count == 0) {
            self.amountLabel.text = @"鱼苗  0 条";
            return;
        }
        Fish *fish = result.data.list.lastObject;
        self.mainLabel.text = FORMAT(@"主力  %@",fish.name);
        [self.mainIcon sd_setImageWithURL:[NSURL URLWithString:fish.img] placeholderImage:[UIImage imageNamed:@"fish1"]];
//        self.miniFishCount = [self fetchMiniFishCount:result];
        [self requestFishDetail:fish];
    }];
}

-(void)requestFishIndex {
    WEAK(self)
    [Api apiFisheryIndex:^(ResponseFisherIndex *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        if (result.data) {
            self.areaLabel.text = result.data.fishery.name;
            self.fishIndex = result.data;
            [self reqeustMiniFishInfo];
        }
    }];
}

-(void)showLoginAlert
{
    RAlertView *alert = [[RAlertView alloc] initWithStyle:ConfirmAlert];
    alert.headerTitleLabel.text = @"Token过期";
    alert.theme = RGB16(0X1abc9c);
    NSMutableString *string = [[NSMutableString alloc]initWithString:@"您在其他地方登录,请重新登录"];
    alert.contentTextLabel.attributedText = [TextHelper attributedStringForString:string lineSpacing:5];;
    [alert.confirmButton setTitle:@"好的" forState:UIControlStateNormal];
    WEAK(self)
    alert.confirm = ^(){
        STRONG(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    };
}

-(void)requestUserInfo {
    WEAK(self)
    [Api apiUserInfoHandler:^(ResponseUserInfo *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            if ([result.code isEqualToString:@"-1"]) {
                [self showLoginAlert];
                return;
            }
            [MFHUDManager showError:result.msg];
            return;
        }
        self.userInfo = result.data;
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:result.data.avator] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
        self.nameLabel.text = result.data.rolename;
        self.powLabel.text =  FORMAT(@"%@",result.data.max_power);
        NSString *ethFloat = FORMAT(@"%.4f",[result.data.eth_amount floatValue]);
        self.ethLabel.text = FORMAT(@"%@",ethFloat);
        [self.countryIcon sd_setImageWithURL:[NSURL URLWithString:result.data.logo] placeholderImage:[UIImage imageNamed:@"icon_china"]];
        [self.mainFishAreaIcon sd_setImageWithURL:[NSURL URLWithString:result.data.logo] placeholderImage:[UIImage imageNamed:@"icon_china"]];
        self.adventure = [self fectchRecmondAdventure:result.data.max_power];
        [self showAdventureUI:self.adventure];
    }];
}

-(void)showAdventureUI:(Adventure*)adventure
{
    [self.oceanIcon sd_setImageWithURL:[NSURL URLWithString:adventure.area_img] placeholderImage:[UIImage imageNamed:@"country_01"]];
    self.fubenNameLabel.text = FORMAT(@"推荐探索  %@",adventure.area_name);
    NSInteger itemsCount = adventure.item.count;
    if (itemsCount == 2) {
        Item *itemETH = adventure.item.lastObject;
        self.ethDesc.text = FORMAT(@"%@",itemETH.eth_amount);
    }
    Item *itemStorne = adventure.item.firstObject;
    [self.stoneIcon sd_setImageWithURL:[NSURL URLWithString:itemStorne.item_img] placeholderImage:[UIImage imageNamed:@"stone"]];
    self.stoneDesc.text = FORMAT(@"%@",itemStorne.item_num);
}


-(void)requestFishDetail:(Fish*)fish {
    WEAK(self)
    [Api apiFishDetail:fish.level type:fish.type career:fish.career handler:^(ResponseFishDetail *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        if (result.data.list.count == 0) {
            return;
        }
        Fish *fish = result.data.list.firstObject;
        self.fish = fish;
//        self.mainFishPowerLabel.text = FORMAT(@"%@ / %@",fish.fight_power,fish.fight_power_limit);
        self.mainFishLevelLabel.text = FORMAT(@"等级  %@",fish.level);
        self.mainFishAreaLabel.text = fish.position;
        [self.mainFishHeadIcon sd_setImageWithURL:[NSURL URLWithString:fish.img] placeholderImage:[UIImage imageNamed:@"fish1"]];
        if (fish.custom_name.length > 0) {
            self.mainFishNickNameLabel.text = FORMAT(@"%@",fish.custom_name);
        }
        else
        {
            self.mainFishNickNameLabel.text = FORMAT(@"%@",fish.name);
        }
        [self requestEvoleAttr:fish];
    }];
}

-(void)requestEvoleAttr:(Fish*)fish{
    WEAK(self)
    [Api apiEvolveAttr:fish.fish_id career:fish.career handler:^(ResponseEvolveAttr *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        Evolve *nextFish = result.data;
        if (!nextFish) {
            return;
        }
        self.mainFishPowerLabel.text = FORMAT(@"%@ / %@",nextFish.fight_power,nextFish.fight_power_limit);
        self.mainFishStoneLabel.text = FORMAT(@"%@ / %@",nextFish.item_have,nextFish.evolution_item_num);
        self.mainFishNextLabel.text  = FORMAT(@"下一级  %@",nextFish.name);
        [self.mainFishNextIcon sd_setImageWithURL:[NSURL URLWithString:nextFish.img] placeholderImage:[UIImage imageNamed:@"fish1"]];
        [self.mainFishStoneIcon sd_setImageWithURL:[NSURL URLWithString:nextFish.item_img] placeholderImage:[UIImage imageNamed:@"stone"]];
        
        
        NSInteger powerLeft = [nextFish.fight_power_limit integerValue]  -  [nextFish.fight_power integerValue];
        NSInteger stoneLeft = [nextFish.evolution_item_num integerValue] - [nextFish.item_have integerValue];
        NSInteger leftFish  = 0;
        if (powerLeft > 0) {
            leftFish = (powerLeft / 3) + 1;
        }
        self.mainFishMiniLabel.text = FORMAT(@"%@ / %@",self.miniFishCount,@(leftFish));
        [self.eatButtonAll setTitle:FORMAT(@"吞噬(%td)",leftFish) forState:UIControlStateNormal];
        self.leftEatFishCount = @(leftFish);
  
        
        if (powerLeft < 0) {
            self.mainFishPowerLabel.textColor = [UIColor greenColor];
            self.isPowerOK = YES;
        }
        else
        {
            self.mainFishPowerLabel.textColor = [UIColor redColor];
            self.isPowerOK = NO;
        }
        
        if (stoneLeft < 0) {
            self.mainFishStoneLabel.textColor = [UIColor greenColor];
             self.isStoneOK = YES;
        }
        else
        {
            self.mainFishStoneLabel.textColor = [UIColor redColor];
             self.isStoneOK = NO;
        }
        
        if ([self.miniFishCount integerValue] - leftFish >= 0) {
            self.mainFishMiniLabel.textColor = [UIColor greenColor];
            self.isEatOK = YES;
        }
        else
        {
            self.mainFishMiniLabel.textColor = [UIColor redColor];
            self.isEatOK = NO;
        }
        
        if (self.isPowerOK == YES && self.isStoneOK == YES) {
            [self.jinghuaButton setBackgroundColor:HEXCOLOR(0xDA70D6)];
            self.jinghuaButton.enabled = YES;
        }
        else
        {
            [self.jinghuaButton setBackgroundColor: [UIColor lightGrayColor]];
            self.jinghuaButton.enabled = NO;
        }
        
    }];
    
}

-(NSNumber*)fetchMiniFishCount:(ResponseMyFishList*)resp {
    NSArray *list = resp.data.list;
    if (list.count == 0) {
        return @(0);
    }
    for (Fish *fish in list) {
        if ([fish.level isEqualToNumber:@(1)]) {
            return [NSNumber numberWithInteger:[fish.count integerValue]];
        }
    }
    return @(0);
}

-(void)showAlertBuyMiniFish:(NSNumber*)count
{
    NSInteger haveCount = [self.miniFishCount integerValue];
    NSInteger leftCount = [count integerValue];
    
    NSInteger shouldBuy = leftCount - haveCount;
    
    CGFloat total = shouldBuy * [self.miniFishPrice floatValue];
    NSNumber *eth = @(total);
    
    RAlertView *alert = [[RAlertView alloc] initWithStyle:CancelAndConfirmAlert];
    alert.headerTitleLabel.text = @"鱼苗不足";
    alert.theme = RGB16(0X1abc9c);
    NSMutableString *string = [[NSMutableString alloc]initWithString:@""];
    [string appendString:FORMAT(@"是否花费 ETH : %.3f 购买 %@ 只鱼苗?",[eth floatValue],@(shouldBuy))];
    
    alert.contentTextLabel.attributedText = [TextHelper attributedStringForString:string lineSpacing:5];;
    [alert.confirmButton setTitle:@"购买" forState:UIControlStateNormal];
    [alert.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    WEAK(self)
    alert.confirm = ^(){
        STRONG(self)
        [self requestBuyMiniFish:@(shouldBuy)];
        
    };
    alert.cancel = ^(){
        
    };
}

-(void)requestBuyMiniFish:(NSNumber*)count {
    FisheryIndex *fishArea = self.fishIndex;
    WEAK(self)
    [Api apiBuyMiniFish:fishArea.fishery.country_id fisheryId:fishArea.fishery.fishery_id count:count handler:^(Response *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        [MFHUDManager showSuccess:result.msg];
        [self requestAll];
    }];
}


- (IBAction)actionEatOne:(id)sender {
    [self requestUserInfo];
    
    if ([self.miniFishCount integerValue] == 0) {
        [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
        [self showAlertBuyMiniFish:@(1)];
        return;
    }
    WEAK(self)
    [Api apiAutoSelect:self.fish.fish_id handler:^(ResponseFishDetail *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        if (result.data.list.count == 0) {
            [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
            return;
        }
        
        Fish *fish = result.data.list.firstObject;
        NSNumber *target_fish_id =  fish.fish_id;
        
        if (![fish.level isEqualToNumber:@(1)]) {
            [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
            [self showAlertBuyMiniFish:@(1)];
            return;
        }
        [Api apiEatFish:self.fish.fish_id target_fishId:target_fish_id count:@(1) handler:^(Response *respBase, NSError *errorBase) {
            if (errorBase) {
                [MFHUDManager showError:@"请检查网络,稍后再试"];
                return;
            }
            if (errorBase.code != 0) {
                [MFHUDManager showError:result.msg];
                return;
            }
            [MFHUDManager showSuccess:respBase.msg];
            [self requestAll];
        }];
    }];
}

-(void)checkAlertBuyETH:(NSNumber*)eth {
    RAlertView *alert = [[RAlertView alloc] initWithStyle:CancelAndConfirmAlert];
    alert.headerTitleLabel.text = @"体力不足";
    alert.theme = RGB16(0X1abc9c);
    NSMutableString *string = [[NSMutableString alloc]initWithString:@""];
    [string appendString:FORMAT(@"是否花费 ETH : %@ 购买体力?",eth)];
    alert.contentTextLabel.attributedText = [TextHelper attributedStringForString:string lineSpacing:5];;
    [alert.confirmButton setTitle:@"购买" forState:UIControlStateNormal];
    [alert.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    WEAK(self)
    alert.confirm = ^(){
        STRONG(self)
        [self requestBuyETH];
    };
    alert.cancel = ^(){

    };
}

-(void)requestCheckETH
{
    WEAK(self)
    [Api apiBuyCheck:^(ResponseBuyCheck *resp, NSError *error) {
        STRONG(self)
        [self checkAlertBuyETH:resp.data];
    }];
}

-(void)requestBuyETH
{
    WEAK(self)
    [Api apiBuy:^(Response *resp, NSError *error) {
        STRONG(self)
        [MFHUDManager showSuccess:resp.msg];
        [self requestAll];
    }];
}


-(void)eatAllFishBig:(NSNumber*)count target_fish_id:(NSNumber*)target_fish_id
{
    [MFHUDManager showLoading:@"吞噬中...."];
    
    const NSInteger perEatCount = 500;
    
    NSInteger fishCount = [count integerValue];
    NSInteger eatCount  = fishCount / perEatCount + 1;
    
    __block NSInteger cnt = eatCount;
    __block NSInteger lastFishCount = fishCount % perEatCount;
    
    NSMutableArray *quene = [[NSMutableArray alloc]init];
    
    for (int i = 0;  i < cnt ; i++) {
        if (i == (cnt - 1)) {
            [quene addObject:@(lastFishCount)];
        }
        else
        {
            [quene addObject:@(perEatCount)];
        }
    }
    
    for (int i = 0;  i < cnt ; i++) {
        WEAK(self)
        [Api apiEatFish:self.fish.fish_id target_fishId:target_fish_id count:quene[i] handler:^(Response *respBase, NSError *errorBase) {
            STRONG(self)
            if (errorBase) {
                [MFHUDManager showError:@"请检查网络,稍后再试"];
                return;
            }
            if (![respBase.code isEqualToString:@"0"]) {
                [MFHUDManager showError:respBase.msg];
                return;
            }
            cnt--;
            if (cnt == 0) {
                [MFHUDManager showSuccess:@"一键吞噬成功"];
                [self requestAll];
            }
        }];
    }
}

- (IBAction)actionEatAll:(id)sender {
    [self requestUserInfo];
    if (self.isEatOK == NO) {
        [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
        [self showAlertBuyMiniFish:self.leftEatFishCount];
        return;
    }
    // 吞噬判断
    WEAK(self)
    [Api apiAutoSelect:self.fish.fish_id handler:^(ResponseFishDetail *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        if (result.data.list.count == 0) {
            [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
            return;
        }
        
        Fish *fish = result.data.list.firstObject;
        NSNumber *target_fish_id =  fish.fish_id;
        if (![fish.level isEqualToNumber:@(1)]) {
            [MFHUDManager showError:@"鱼苗不足,请购买鱼苗"];
            [self showAlertBuyMiniFish:self.leftEatFishCount];
            return;
        }
        
        // 大于500只鱼的处理
        if ([self.leftEatFishCount integerValue] > 500) {
            [self eatAllFishBig:self.leftEatFishCount target_fish_id:target_fish_id];
            return;
        }
        
        [Api apiEatFish:self.fish.fish_id target_fishId:target_fish_id count:self.leftEatFishCount handler:^(Response *respBase, NSError *errorBase) {
            if (errorBase) {
                [MFHUDManager showError:@"请检查网络,稍后再试"];
                return;
            }
            if (![respBase.code isEqualToString:@"0"]) {
                [MFHUDManager showError:respBase.msg];
                return;
            }
            [MFHUDManager showSuccess:respBase.msg];
            [self requestAll];
            
        }];
    }];
}
- (IBAction)actionJingHua:(id)sender {
    
    [MFHUDManager showLoading:@"进化中...."];
    
    WEAK(self)
    [Api apiJinghua:self.fish.fish_id handler:^(Response *result, NSError *error) {
        STRONG(self)
        if(error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [MFHUDManager showError:result.msg];
            return;
        }
        [MFHUDManager showSuccess:result.msg];
        [self requestAll];
    }];
}

@end
